Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[kernel,libc] Rewrite kernel brk/sbrk and stack_check, fix C library malloc #2128

Merged
merged 2 commits into from
Dec 8, 2024

Conversation

ghaerr
Copy link
Owner

@ghaerr ghaerr commented Dec 8, 2024

Rewrites sys_brk, sys_sbrk and stack_check with better error checking and new messages for heap and stack problems.

Also fixes a somewhat major problem with C library malloc which caused an extra 1024 bytes to be allocated from the heap for any allocation request >= 1024, which effectively doubled the size of the heap necessary to fulfill the request!

The C library malloc routine now allocates all requests less than 32 bytes as 32 bytes from the heap for later subdivision, but all larger requests without chunking up to a large (previously 1024 byte) chunk size. In addition, when memory is low, malloc doesn't retry three times which produced multiple failure messages, usually right before a stack overflow message. That has been fixed for the most part, except for when an application uses large stack buffers without stack checking turned on (more coming on that in next steps).

Removes unused CONFIG_EXEC_LOW_STACK option from loader.

These changes were you used to test the 8086 toolchain and resulted in PR rafael2k/8086-toolchain#17.

Now, when applications run out of heap or stack, or the stack pointer moves into the unused heap area, more informative messages are produced by the kernel, to let the developer know what is going on.

Following are the changed error messages:

When the stack usage grows outside (below) its normally reserved and protected area into the heap, the following messages are produced:

"STACK USING 1131 UNUSED HEAP" when the stack isn't large enough and unused heap area is used, or
"STACK OVERFLOW" when the stack grew into an inuse area of the heap. In this case the process is also terminated.

When a request is made by malloc to get more memory from the heap, and that fails, the message

"SBRK 1024 FAIL, OUT OF HEAP SPACE" is displayed, and malloc returns NULL.

If the stack has already grown into the unused heap, but a malloc allocation request wants some of that heap area, the message

"SBRK %d FAIL, WOULD OVERWRITE STACK" is displayed.

This may all sound complicated, but in the end more information is displayed, and when the messages are displayed, it gives better information as to whether the program's heap or stack should be increased. This was definitely needed for our upcoming C86 toolchain, as some tools require maximum heap or stack space.

To see detailed SBRK/BRK allocation information on the console, set debug=1 in /bootopts.

@ghaerr ghaerr merged commit c364a15 into master Dec 8, 2024
2 checks passed
@ghaerr ghaerr deleted the lowstack branch December 8, 2024 01:31
@rafael2k
Copy link
Contributor

rafael2k commented Dec 9, 2024

This really helps to develop the toolchain. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants